package com.qf.controller;

import com.qf.Excaption.MyExcaption;
import com.qf.domain.NewsDetail;
import com.qf.service.NewsCommentService;
import com.qf.service.NewsDetailService;
import com.qf.service.impl.NewsCommentServiceimpl;
import com.qf.vo.ResultVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@ResponseBody
@RequestMapping("/detail")
public class NewsDetailController {
    @Autowired(required=true)
    private NewsDetailService newsDetailService;

    @GetMapping("/list")
    public ResultVO selectAll(@RequestParam(defaultValue = "1")Integer page,
                              @RequestParam(value = "limit",defaultValue = "5")Integer size,
                              String title){
        System.out.println(title);
        List<NewsDetail> list=newsDetailService.selectAll(page,size,title);
        System.out.println(list.toString());
        if (list!=null){
            ResultVO resultVO=new ResultVO();
            resultVO.setData(list);
            return resultVO;
        }else {
            throw new MyExcaption("没有查到新闻",500);
        }
    }
    @PostMapping("/deletenews")
    public ResultVO deletenews(@Param("id")Integer id){
        ResultVO resultVO=new ResultVO();
        if (id!=null){
            int i=newsDetailService.deletenews(id);
            if (i==1) {
                return resultVO;
            }else {
                throw new MyExcaption("数据有误，无法删除",500);
            }
        }else {
            resultVO.setCode(500);
            resultVO.setMsg("删除失败");
            return  resultVO;
        }


    }
}
